const express=require('express')
const comment=express.Router()

const axios=require('axios')

//遍历主评论
comment.get('/comment/searchCommentByForumId',(req,res)=>{
    let {forumId,pageNow,size}=req.query
    console.log(pageNow);
    axios({
        url:'/comment/searchCommentByForumId',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            forumId:forumId,
            pageNum:pageNow,
            size:size||6
        }
    }).then(value=>{
        console.log(value.data);
        for(let i=0;i<value.data.data.list.length;i++){
            value.data.data.list[i].sonComment.list=value.data.data.list[i].sonComment.list.slice(0,1)
        }
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//删除评论
comment.get('/comment/delCommentById',(req,res)=>{
    let {formId,commentId,userId}=req.query
    console.log(formId);
    console.log(commentId);
    console.log(userId);
    axios({
        url:'/comment/delCommentByCommentId',
        method:"delete",
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params:{
            userId:userId,
            formId:formId,
            commentId:commentId
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//主评论
comment.get('/comment/insertComment',(req,res)=>{
    let {content,forumId,ownerId}=req.query
    axios({
        url:'/comment/insertComment',
        method:"post",
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            content:content,
            ownerId:ownerId,
            forumId:forumId
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//子评论
comment.get('/comment/childeComment',(req,res)=>{
    let {content,forumId,ownerId,replyId}=req.query
    axios({
        url:'/comment/insertComment',
        method:"post",
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            content:content,
            ownerId:ownerId,
            forumId:forumId,
            replyId:replyId,
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//遍历子评论
comment.get('/comment/searchCommentOfRootComment',(req,res)=>{
    let {id,pageNum,size}=req.query
    axios({
        // url:'/comment/searchCommentOfRootComment',
        url:'/comment/getSonComment',
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            fatherId:id,
            pageNum:pageNum||1,
            size:size||10
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

//对子评论评论
comment.get('/comment/childeChilde',(req,res)=>{
    let {content,forumId,ownerId,replyId,repliedOwnerId,rootId}=req.query
    axios({
        url:'/comment/insertComment',
        method:"post",
        headers: {
            'Content-Type':'application/x-www-form-urlencoded',
        },
        params:{
            content:content,
            ownerId:ownerId||req.session.userMessage.id,
            forumId:forumId,
            replyId:replyId,
            repliedOwnerId:repliedOwnerId,
            rootId:rootId
        }
    }).then(value=>{
        res.status(200).json({
            key:value.data
        })
    }).catch(err=>{
        res.status(200).json({
            error:err
        })
    })
})

module.exports=comment